using System;
using FirebirdSql.Data.FirebirdClient;
using Prologis.DAL;
using System.Data;


namespace Prologis.ShifterNT.Datasource
{

    public class DbDataSource : DataSourceSkeleton
    {

        public IDbHelperWrapper DbHelper { get; set; }
        private IDataReader dataReader { get; set; }
        
        //IDbHelperWrapper DbHelper = DbHelperFactory.GetDbHelper("Global.AppSettings.DbSetting");
        
        public string  ConnectionString { get; set; }
        public string ProviderName { get; set; }
        public string TableName { get; set; }



        public DbDataSource()
        {
            string s = "";
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        public override bool Active
        {
            set
            {
                if (base.Active != value)
                {
                    base.Active = value;
                    if (value)
                    {
                        this.DbHelper = DbHelperFactory.GetDbHelper(this.ConnectionString, this.ProviderName);
                        this.Count = (int)this.DbHelper.ExecuteScalar(CommandType.Text, "select count(*) from " + this.TableName);
                        this.dataReader = this.DbHelper.ExecuteDataReader(CommandType.Text, "select * from " + this.TableName);
                    }
                    else
                    {
                        //TODO: Nullsetzen notwendig ?
                        this.DbHelper = null;

                    }
                }
            }
        }


       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fieldList"></param>
        /// <returns></returns>
        /// 
        public override MoveNextDecision GetNext(ref FieldList fieldList)
        {

            if (this.dataReader.Read())
            {
                fieldList = Tokenizer.Tokenize(String.Empty, StringArrayBuilder.GetArray(dataReader));
                return base.GetNext(ref fieldList);
            }
            else
            {
                return MoveNextDecision.EOS;
            }
        }


    }
}

